<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\LaborCompany;
use App\Models\ResourceBind;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaborCompanyService
{
    public function get($title)
    {
        $sql = LaborCompany::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        $files = [];
        if(!empty($res['files'])){
            $files = explode(',',$res['files']);
        }
        if(!empty($res['business_cert'])){
            $business_cert = explode(',',$res['business_cert']);
            $files = array_merge($files,$business_cert);
        }
        if(!empty($res['security_cert'])){
            $security_cert = explode(',',$res['security_cert']);
            $files = array_merge($files,$security_cert);
        }
        if(!empty($res['legal_cert'])){
            $legal_cert = explode(',',$res['legal_cert']);
            $files = array_merge($files,$legal_cert);
        }
        if(!empty($res['safety_cert'])){
            $safety_cert = explode(',',$res['safety_cert']);
            $files = array_merge($files,$safety_cert);
        }
        if(!empty($res['human_resources_cert'])){
            $human_resources_cert = explode(',',$res['human_resources_cert']);
            $files = array_merge($files,$human_resources_cert);
        }

        try {
            $data = DB::transaction(function () use ($res,$files) {
                $data = LaborCompany::create($res);

                $bind_insert = [];
                foreach ($files as $k => $v) {
                    $bind = [];
                    $bind['source_id'] = $data->id;
                    $bind['resource_id'] = $v;
                    $bind['model'] = 'LaborCompany';
                    $bind_insert[] = $bind;
                }
                ResourceBind::insert($bind_insert);
                //添加到人员库存
                if($data){
                    return $data->id;
                }
            });

            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);

        $files = [];
        if(!empty($res['files'])){
            $files = explode(',',$res['files']);
        }
        if(!empty($res['business_cert'])){
            $business_cert = explode(',',$res['business_cert']);
            $files = array_merge($files,$business_cert);
        }
        if(!empty($res['security_cert'])){
            $security_cert = explode(',',$res['security_cert']);
            $files = array_merge($files,$security_cert);
        }
        if(!empty($res['legal_cert'])){
            $legal_cert = explode(',',$res['legal_cert']);
            $files = array_merge($files,$legal_cert);
        }
        if(!empty($res['safety_cert'])){
            $safety_cert = explode(',',$res['safety_cert']);
            $files = array_merge($files,$safety_cert);
        }
        if(!empty($res['human_resources_cert'])){
            $human_resources_cert = explode(',',$res['human_resources_cert']);
            $files = array_merge($files,$human_resources_cert);
        }

        try {

            $data = DB::transaction(function () use ($input,$res,$files) {
                $data = LaborCompany::where('id',$input['id'])->update($res);
                $bind_insert = [];
                foreach ($files as $k => $v) {
                    $bind = [];
                    $bind['source_id'] = $input['id'];
                    $bind['resource_id'] = $v;
                    $bind['model'] = 'LaborCompany';
                    $bind_insert[] = $bind;
                }
                ResourceBind::where('source_id',$input['id'])->where('model','LaborCompany')->delete();
                if(count($bind_insert)){
                    ResourceBind::insert($bind_insert);
                }

                if($data){
                    return $input['id'];
                }
            });

            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

}